package cn.mw.cmdb.enums;

import cn.hutool.json.JSONArray;
import cn.mw.cmdb.entity.AlgorithmInfo;
import cn.mw.cmdb.service.impl.AlgorithmInfoServiceImpl;
import com.alibaba.fastjson.JSONObject;

public enum AlgorithmEnum {
    busyness("繁忙度", "busyness") {
        @Override
        public String generatorAlgorithm(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            return service.calculationBusyness(info);
        }

        @Override
        public void saveInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            String busyness = service.calculationBusyness(info);
            info.setCalculationMethod(busyness);
        }

        @Override
        public void updateInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            service.calculationBusyness(info);
        }
    }, availability("可用性", "availability") {
        @Override
        public String generatorAlgorithm(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            return service.calculationAvailability(info);
        }

        @Override
        public void saveInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            info.setCalculationMethod(service.calculationAvailability(info));
        }

        @Override
        public void updateInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            service.calculationAvailability(info);
        }
    }, healthiness("健康度", "healthiness") {
        @Override
        public String generatorAlgorithm(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            return service.calculationHealthiness(info);
        }

        @Override
        public void saveInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            info.setCalculationMethod(service.calculationHealthiness(info));
        }

        @Override
        public void updateInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info) {
            service.calculationHealthiness(info);
        }
    };

    private static final String KEY = "algorithmType";

    private final String chineseName;
    private final String englishName;


    AlgorithmEnum(String chineseName, String englishName) {
        this.chineseName = chineseName;
        this.englishName = englishName;
    }

    public String getChineseName() {
        return chineseName;
    }

    public String getEnglishName() {
        return englishName;
    }

    public abstract String generatorAlgorithm(AlgorithmInfoServiceImpl service, AlgorithmInfo info);

    public abstract void saveInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info);

    public abstract void updateInfoByType(AlgorithmInfoServiceImpl service, AlgorithmInfo info);

    public static AlgorithmEnum getAlgorithmEnum(String type) {
        return AlgorithmEnum.valueOf(type);
    }

    public static JSONObject getAll() {
        JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (AlgorithmEnum value : AlgorithmEnum.values()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("chineseName", value.getChineseName());
            jsonObject.put("englishName", value.getEnglishName());
            jsonArray.add(jsonObject);
        }
        object.put(KEY, jsonArray);
        return object;
    }
}
